import { Component, OnInit, Injector } from '@angular/core';
import { CityListComponent } from '../../school/cityList';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { HttpClient } from "@angular/common/http";
import { Router, ActivatedRoute, Params } from '@angular/router';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
@Component({
  selector: 'app-classaddmanagement',
  templateUrl: './classaddmanagement.component.html',
  styleUrls: ['./classaddmanagement.component.scss']
})
export class ClassaddmanagementComponent extends NgZorroPageBaseComponent implements OnInit {
  classid;
  className; // 班级
  remark; // 备注
  project = []; // 项目
  departmentName; // 系
  allstudentdata;
  checkOptionsOne = [
    { label: 'Apple', value: 'Apple', checked: false },
  ];
  radioValue = 'true';
  selectedProvince;
  selectedCity;
  cityData: any;
  time = "2018-07-30";
  schoolName;
  personName = "";
  personPhone = "";
  personEmail = "";
  personUserName = "";
  personPassword = "";
  collegelName;
  provinceData: any;
  mess: any;
  resute = [];
  resute1 = [];
  resultxy = [];
  listEntitiesscl;
  timelist = new Array();
  ilist = 0;
  listEntitiesdata: any;
  aclear;
  listprodata;
  classproject = [];
  projectIdarr = [];
  lstCheckedId = [];
  projectadd = [];
  ngOnInit() {
    this.activatedRoute.queryParams.subscribe(
      data => this.classid = (data['classId']));
    if (this.classid !== "add") {
      this.http1.get<any>(`ClassInfo/GetClassInformation?aid=${this.classid}`).then(e => {
        this.className = e[0].className;
        this.departmentName = e[0].collegeDepartmentId;
        this.remark = e[0].description;
        e[0].project.forEach((element, index) => {
          if (element.isChecked) {
            this.projectIdarr.push(element.projectId);
            this.classproject[element.projectId] = true;
          }

        });
        if (this.projectIdarr[0]) {
          this.projectIdarr.forEach(prolist => {
            this.lstCheckedId.push(prolist);
          });
        }

      });
    }

    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });



  }
  constructor(injecter: Injector,
    public http1: HttpService,
    public http: HttpClient,
    private activatedRoute: ActivatedRoute,
  ) {
    super(injecter,
      new BaseComponentConfig(
        "School/GetAllDepartment",
        "",
        "",
        "",
        "",
        "id",
        "",
        "学校管理",
        [],
        "",
        { Edit: false, Dell: false },
      ));
    const city = new CityListComponent;
    this.cityData = city.cityDatas;
    this.provinceData = city.provinceDatas;


    const datalist1 = {
      "currentPageIndex": 1,
      "pageSize": 10,
      "expressionPairs": [
      ],
      "sortExpressions": [
      ]
    };
    this.http1.get<any>('SchoolAcademeManager/GetSchoolAdminProjects').then(e => {
      this.listprodata = e;
      // this.listprodata.forEach((element, indedx) => {
      //   this.classproject[indedx] = false;
      // });
    });




  }


  provinceChange(value: string): void {
    this.selectedCity = this.cityData[value][0];
  }
  /**
   * 保存學校信息
   *
   */


  async serve(type: string) {


    const that = this;

    const data = {
      "className": this.className, "collegeDepartmentId": this.departmentName, "description": this.remark
    };
    if (this.classid !== "add") {
      this.project = this.lstCheckedId;
      data["id"] = that.classid;
      data["drillProjectIdList"] = this.project;
      if (this.className === "" || this.departmentName === "undefined" ||
        this.remark === "" || this.project[0] === "undefined" || !this.departmentName || !this.project[0]) {
        that.message.info('请填写完整');
        return false;
      } else {
        await that.http1.post<any>("ClassInfo/Update", data);
      }
    } else {
      data["drillProjectIdList"] = this.projectadd;
      if (this.className === "" || this.departmentName === "undefined" ||
        this.remark === "" || !this.departmentName || this.projectadd[0] === "undefined" || !this.projectadd[0]) {
        that.message.info('请填写完整');
        return false;
      } else {
        await that.http1.post<any>("ClassInfo/Create", data);
      }
    }

    this.message.create(type, `保存成功`);

    this.router.navigate(['app/classindex']);

  }

  delete() {

  }
  async provinceChangesol(e) {
    this.departmentName = e;
  }
  async projectdata(value: string[]) {
    this.projectadd = value;

  }


  async searchChange(searchText: string) {
    const that = this;
    clearTimeout(this.aclear);
    this.aclear = setTimeout(() => {
      that.sealist(searchText);
    }, 1000);


  }


  sealist(i) {
    const datalist = { "Name": i };
    this.listEntitiesdata = [];
    this.http1.post<any>("School/GetSchoolToTen", datalist).then(e => {
      this.listEntitiesdata = e;

    });
  }

  classpd(id) {
    if (this.lstCheckedId.indexOf(id) < 0) {
      this.lstCheckedId.push(id);
    } else {
      this.lstCheckedId = this.lstCheckedId.filter(e => e !== id);
    }
  }
}

